package com.zm.Study02_FileInputStreamTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamDome01 {
    FileInputStream fis=new FileInputStream("D:\\project\\IDEAPROJECT\\JavaIO\\src\\main\\resources\\static\\FileInputStreamTxt\\fis.txt");

    public FileInputStreamDome01() throws FileNotFoundException {
    }

    public static void main(String[] args) throws IOException {
       FileInputStreamDome01 fileInputStreamDome01=new FileInputStreamDome01();
      // fileInputStreamDome01.read();
      // fileInputStreamDome01.readWhile();
        fileInputStreamDome01.readWhile2();

    }

    //read（）读取单个字符，读到末尾返回-1
    public void read() throws IOException {
        //读取a
        int text1=fis.read();
        System.out.println(text1);
        System.out.println((char) text1);
        System.out.println("---------------------");
        //读取b
        int text2=fis.read();
        System.out.println(text2);
        System.out.println((char) text2);
        System.out.println("---------------------");

        //fos中仅一个“a",督导末尾会返回  -1
        int text3=fis.read();
        System.out.println(text3);
        System.out.println((char) text3);
        fis.close();
    }

    //read改进（while循环）
    public void readWhile() throws IOException {
        int by=fis.read();
        while (by!=-1){
         //   System.out.print( by);
            System.out.print( (char)by);
            by=fis.read();

        }
        fis.close();
    }

    //read改进（while循环改进）
    public void readWhile2() throws IOException {
        int by;
        while ((by=fis.read())!=-1){
          //  System.out.println(by);
            System.out.print( (char)by);
        }
        fis.close();
    }
}
